package com.loanapp.service;

import java.math.BigDecimal;

import javax.jms.MapMessage;
import javax.jms.Message;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;

import com.loanapp.domain.LoanApplication;

public class JmsReceiverCreditProcess {

	private static Log log = LogFactory.getLog(JmsReceiverCreditProcess.class);

	private JmsTemplate jmsTemplate;

	/**
	 * @return Returns the jmsTemplate.
	 */
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}

	/**
	 * @param jmsTemplate
	 *            The jmsTemplate to set.
	 */
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public LoanApplication receiveMessage() {
		Message msg = jmsTemplate.receive();
		LoanApplication loanApp = new LoanApplication();
		try {
			MapMessage mapMessage = (MapMessage) msg;
			if (msg != null) {
				log.debug("Credit Request Message Received: "
						+ mapMessage.toString());
			}
			loanApp.setLoanAppId(mapMessage.getLong("loanAppId"));
			loanApp.setFirstName(mapMessage.getString("borrowerFirstName"));
			loanApp.setLastName(mapMessage.getString("borrowerLastName"));
			loanApp.setSsn(mapMessage.getString("ssn"));
			loanApp.setExpirationDate(mapMessage.getString("expirationDate"));
			loanApp.setLoanAmount(new BigDecimal(mapMessage
					.getDouble("loanAmount")));
			loanApp.setCreditScore(mapMessage.getInt("creditScore"));

		} catch (Exception e) {
			log.error(e, e);
		}

		return loanApp;
	}
}
